//脚本模块管理基类
import CScriptMgr from "./cSpriteMgr"
import explorer from "../librarysapi/api_explorer";
import aconsole from "../librarysapi/api_console";
import changesc from "../librarysapi/api_chagnescreen"
import cEnumMgr from "../Enum/cEnumMgr";
export default class cModuleMgr extends CScriptMgr {
    protected static g_cModules:cModuleMgr[]    = [];           //[模块][全局]
    protected static g_sMode:string             = null;         //[横屏][竖屏]
    protected sTargetMode:string                = null;         //[屏幕]
    protected screen:cc.Node                    = null;         //场景节点
    protected onself:cc.Node                    = null;         //场景节点
    protected spanel:string                     = null;         //预制路径

    //注册模块
    constructor(name:string){
        super(name);
    }


    //获取模块
    public getModule(name:string):any{
        return CScriptMgr.getModule(name?name:this.name);
    }

    //获取挂载
    public getMount(uuid:string):any{
        return CScriptMgr.getMount(uuid?uuid:this.onself.uuid);
    }

     //面板预制
     public setSpanel(url:string):void{
        this.spanel = url
    }

    public node():cc.Node{
        return this.onself;
    }

     //指定节点
     public settingnode(index:number):void{
        let nodes = [
            "Canvas/node0",
            "Canvas/node1",
            "Canvas/node2",
            "Canvas/node3",
            "Canvas/node4",
            "Canvas/node5"
        ];
        this.screen = cc.find(nodes[index]);
    }

    //指定节点
    public settingscreen(node:cc.Node):void{
        this.screen = node;
    }

    //竖屏节点
    protected portraitscreen():void{
        this.sTargetMode = cEnumMgr.prortrait;
    }
    //横屏节点
    protected landscapescreen():void{
        this.sTargetMode = cEnumMgr.landscape;
    }

    //调整节点
    protected changescreen():void{
        //没有目标
        if(null == this.sTargetMode){
            return
        }
        //屏幕模式
        if(cModuleMgr.g_sMode != this.sTargetMode){
            cModuleMgr.g_sMode = this.sTargetMode;
            //转成竖屏
            if(this.sTargetMode == cEnumMgr.prortrait){
                changesc.changeCanvasToVertical()
            }
            //转成横屏
            if(this.sTargetMode == cEnumMgr.landscape){
                changesc.changeCanvasToHorizontal()
            }
        }
    }

   

    //实例化
    protected changeInstance():void{
    }

    protected extendStart():void{
    }

    protected extendClose():void{
    }

    //切换
    public changeStart():void{
        if(!this.onself){
            explorer.locaLoading(this.spanel,cc.Prefab,null,(error: Error, prefab: any)=>{
                if(!error){
                    this.onself = cc.instantiate(prefab);
                    this.screen.addChild(this.onself);
                    let uuid = this.onself.uuid;
                    CScriptMgr.g_mapScripts[uuid] = this;
                    this.startEffect(()=>{
                        this.changeInstance();
                        this.changescreen();
                        this.startComplete();
                        this.extendStart();
                    });
                }
            });
        }
        else{
            if(!this.onself.active){
                this.onself.active = true;
                this.startEffect(()=>{
                    this.changescreen();
                    this.startComplete();
                    this.extendStart();
                });
            }
        }
    }

    //关闭
    public changeClose():void{
        let node = this.onself;
        if(node){
            this.closeEffect(function(){
                node.active = false;
                this.extendClose();
            }.bind(this));
        }
        else{
            aconsole.error("this.onself inexistence")
        }
    }

    //启用
    public startComplete(){
        if(this.sTargetMode){
            let scenes = cModuleMgr.g_cModules
            if(scenes.length > 0){
                let befor = scenes.pop();
                befor.changeClose();
            }
            scenes.push(this);
        }
    }
    //关闭
    public closeComplete(){
    }

    //启动效果
    public startEffect(cb){
        cb();
    }

    //关闭效果
    public closeEffect(cb){
        cb();
    }

    //清除脚本
    public clearScripts():any{
        CScriptMgr.g_mapScripts[this.name] = null;
        CScriptMgr.g_mapMounts[this.onself.uuid] = null;
        CScriptMgr.g_mapScripts[this.onself.uuid] = null;
    }

     //逻辑脚本时间派遣
     public dispatchs(data:any) {
    }
}